package ferp.core.player;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * User: olegs
 * Date: 2/17/14 5:04 PM
 */
public class Signature
{
  public static boolean verify(String signature, String content)
  {
    return signature.equals(generate(content));
  }

  public static String generate(String content)
  {
    try
    {
      MessageDigest md = MessageDigest.getInstance("MD5");
      String extended = S + content + S;

      md.reset();

      return new BigInteger(1, md.digest(extended.getBytes())).toString(16);
    }
    catch (NoSuchAlgorithmException e)
    {
      return "";
    }
  }

  private static final String S = "Hello, Dexter Morgan!";

/*
  public static void main(String[] argv)
  {
    Random random = new Random();

    for (int i = 0; i < 100; ++i)
    {
      Profile p = new Profile();

      p.nickname = "Oleg" + random.nextInt(1000);

      String content = Game.gson.toJson(p);
      String signature = Signature.generate(content);

      System.out.println("Signature = " + signature);

      Signature.verify(signature, content);
    }
  }
*/
}
